package com.pass.util.cart;

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

public class CartAnimUtil {

    public static void add2Cart(final AppCompatActivity appCompatActivity, final ImageView goodView, final String imgPath, int[] endLocation, IAnimFinish iAnimFinish) {

        final int[] startLocation = new int[2];
        goodView.getLocationInWindow(startLocation);
        ViewGroup anim_mask_layout = createAnimLayout(appCompatActivity);
        ImageView movedView = new ImageView(appCompatActivity);
        Glide.with(appCompatActivity)
                .load(imgPath)
                .into(movedView);
        anim_mask_layout.addView(movedView);
        setCartParams(goodView, movedView, startLocation);
        // 计算位移
        int endX = endLocation[0] - startLocation[0];// 动画位移的X坐标
        int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
        TranslateAnimation translateAnimationX = new TranslateAnimation(0,
                endX, 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
                0, endY);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        final AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(800);// 动画的执行时间
        movedView.startAnimation(set);
        // 动画监听事件
        set.setAnimationListener(new Animation.AnimationListener() {
            // 动画的开始
            @Override
            public void onAnimationStart(Animation animation) {
                movedView.setVisibility(View.VISIBLE);
                //    Log.e("动画","asdasdasdasd");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // ignore.
            }

            // 动画的结束
            @Override
            public void onAnimationEnd(Animation animation) {
                movedView.setVisibility(View.GONE);
                set.cancel();
                animation.cancel();
                iAnimFinish.onEnd();
            }
        });

    }

    /**
     * @Description: 创建动画层
     */
    private static ViewGroup createAnimLayout(AppCompatActivity appCompatActivity) {
        ViewGroup rootView = (ViewGroup) appCompatActivity.getWindow().getDecorView();
        LinearLayout animLayout = new LinearLayout(appCompatActivity);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setId(Integer.MAX_VALUE);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }

    private static void setCartParams(final ImageView goodView, final ImageView copiedView,
                                      int[] location) {
        int x = location[0];
        int y = location[1];
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
//                ViewGroup.LayoutParams.WRAP_CONTENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT
//        );
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                goodView.getWidth(),
                goodView.getHeight()
        );
        lp.leftMargin = x;
        lp.topMargin = y;
        copiedView.setLayoutParams(lp);
    }


}
